package ai.think.cloud.chat.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;

public class KCV3DES {
    public static void main(String[] args) throws Exception {
        // 1. 原始 16 字节
        byte[] key16 = hex("");
        // 2. 补成 24 字节：K1K2K1
        byte[] key24 = Arrays.copyOf(key16, 24);
        System.arraycopy(key16, 0, key24, 16, 8);

        Cipher c = Cipher.getInstance("DESede/ECB/NoPadding");
        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key24, "DESede"));
        byte[] cipher = c.doFinal(new byte[8]);

        byte[] kcv = Arrays.copyOf(cipher, 3);
        System.out.println("3DES KCV = " + bytesToHex(kcv));  // 例：C4A76D
    }

    private static String bytesToHex(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (byte v : b) sb.append(String.format("%02X", v));
        return sb.toString();
    }
    private static byte[] hex(String s) {
        int len = s.length();
        byte[] out = new byte[len / 2];
        for (int i = 0; i < len; i += 2)
            out[i/2] = (byte) Integer.parseInt(s.substring(i, i + 2), 16);
        return out;
    }
}